/*
 * DialogNewWebinar.java
 *
 * Created on 14-nov-2011, 22:26:30
 */
package gui;

import domein.ImageFilter;
import domein.Webinar;
import domein.WebinarController;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.util.Calendar;
import java.sql.Date;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.sql.Time;

/**
 *
 * @author Robin
 */
public class DialogNewWebinar extends java.awt.Dialog {

    /** Creates new form DialogNewWebinar */
    private ImageIcon ico;
    private File file;
    private WebinarController wc;
    private Webinar webinar;
    private int gebruikerID;

    public DialogNewWebinar(java.awt.Frame parent, boolean modal, int gid) {
        super(parent, modal);
        initComponents();
        file = new File("");


        //webinarcontroller instantie
        wc = WebinarController.getInstance();
        gebruikerID = gid;
        Calendar cal = Calendar.getInstance();
        txtVanU.setValue(cal.getTime().getHours());
        txtVanMin.setValue(cal.getTime().getMinutes());
        txtTotU.setValue(cal.getTime().getHours());
        txtTotMin.setValue(cal.getTime().getMinutes());

        this.setTitle(gid + " ");

        //positie panel:
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension screenSize = tk.getScreenSize();
        int screenWidth = screenSize.width;

        setLocation((screenWidth - 824) / 2, 100);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnAnnuleer = new javax.swing.JButton();
        btnMaken = new javax.swing.JButton();
        txtOnderwerp = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtCommentaar = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDatum = new com.toedter.calendar.JDateChooser();
        btnIcon = new javax.swing.JButton();
        txtVanU = new com.toedter.components.JSpinField();
        txtVanMin = new com.toedter.components.JSpinField();
        txtTotU = new com.toedter.components.JSpinField();
        txtTotMin = new com.toedter.components.JSpinField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblErrDatum = new javax.swing.JLabel();
        lblErrNaam = new javax.swing.JLabel();
        lblErrComm = new javax.swing.JLabel();

        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setLocationRelativeTo(null);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btnAnnuleer.setLabel("Annuleren");
        btnAnnuleer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnnuleerActionPerformed(evt);
            }
        });
        jPanel1.add(btnAnnuleer);

        btnMaken.setLabel("Aanmaken");
        btnMaken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMakenActionPerformed(evt);
            }
        });
        jPanel1.add(btnMaken);

        txtOnderwerp.setHorizontalAlignment(javax.swing.JTextField.LEFT);

        jLabel1.setText("Naam webinar:");

        jLabel2.setText("Opmerkingen:");

        txtCommentaar.setColumns(20);
        txtCommentaar.setLineWrap(true);
        txtCommentaar.setRows(5);
        jScrollPane1.setViewportView(txtCommentaar);

        jLabel3.setText("Datum:");

        jLabel4.setText("Van:");

        jLabel5.setText("Tot:");

        txtDatum.setToolTipText("ex.: 12-02-2012");
        txtDatum.setDateFormatString("d-M-yyyy");

        btnIcon.setBackground(new java.awt.Color(255, 255, 255));
        btnIcon.setText("Afbeelding toevoegen");
        btnIcon.setBorder(javax.swing.BorderFactory.createEtchedBorder(null, new java.awt.Color(0, 0, 0)));
        btnIcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIconActionPerformed(evt);
            }
        });

        txtVanU.setMaximum(23);
        txtVanU.setMinimum(0);
        txtVanU.setForeground(new java.awt.Color(0, 0, 0));
        txtVanU.setMaximum(23);
        txtVanU.setMinimum(0);

        txtVanMin.setForeground(new java.awt.Color(0, 0, 0));
        txtVanMin.setMaximum(59);
        txtVanMin.setMinimum(0);

        txtVanU.setMaximum(23);
        txtVanU.setMinimum(0);
        txtTotU.setForeground(java.awt.Color.black);
        txtTotU.setMaximum(23);
        txtTotU.setMinimum(0);

        txtTotMin.setForeground(java.awt.Color.black);
        txtTotMin.setMaximum(59);
        txtTotMin.setMinimum(0);

        jLabel6.setText("U.");

        jLabel7.setText("Min.");

        jLabel8.setText("U.");

        jLabel9.setText("Min.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtOnderwerp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblErrComm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lblErrNaam, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(113, 113, 113)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(6, 6, 6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtTotU, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(txtVanU, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel8))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtVanMin, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                                            .addComponent(txtTotMin, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel9)
                                            .addComponent(jLabel7)))
                                    .addComponent(txtDatum, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblErrDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGap(32, 32, 32))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblErrDatum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtDatum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtVanMin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtVanU, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTotMin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtTotU, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtOnderwerp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblErrNaam, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblErrComm, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btnIconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIconActionPerformed
        JFileChooser fc = new JFileChooser();
        ImagePreviewPanel preview = new ImagePreviewPanel(fc);
        fc.setAccessory(preview);
        fc.addPropertyChangeListener(preview);
        fc.setFileFilter(new ImageFilter());
        int returnVal = fc.showOpenDialog(DialogNewWebinar.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            file = fc.getSelectedFile();


            ico = new ImageIcon(file.getPath());

            ImageIcon tmpIcon = new ImageIcon(file.getPath());

            if (tmpIcon != null) {
                if (tmpIcon.getIconWidth() > 150) {
                    ico = new ImageIcon(tmpIcon.getImage().getScaledInstance(150, -1, Image.SCALE_DEFAULT));
                } else { //no need to miniaturize
                    ico = new ImageIcon(tmpIcon.getImage().getScaledInstance(150, -1, Image.SCALE_DEFAULT));
                }
            }
            btnIcon.setText(null);
            btnIcon.setIcon(ico);

        }
    }//GEN-LAST:event_btnIconActionPerformed

    private void btnMakenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMakenActionPerformed

        if (veldenValideren()) {
            String onderwerp = txtOnderwerp.getText();
            String commentaar = txtCommentaar.getText();
            String afbeelding = "";
            if (!(file.getPath().equals(""))) {
                afbeelding = file.getPath();
            } else {
                afbeelding = "src/images/java.jpg";
            }

            java.sql.Date datum = new Date(txtDatum.getDate().getTime());
            java.sql.Time van = Time.valueOf(txtVanU.getValue() + ":" + txtVanMin.getValue() + ":" + 0);
            java.sql.Time tot = Time.valueOf(txtTotU.getValue() + ":" + txtTotMin.getValue() + ":" + 0);
            int instructeur = gebruikerID;
            webinar = new Webinar(onderwerp, commentaar, afbeelding, datum, van, tot, false, instructeur, false);
            wc.voegWebinarToe(webinar);
            dispose();
        }

    }//GEN-LAST:event_btnMakenActionPerformed

    private void btnAnnuleerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnnuleerActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_btnAnnuleerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnnuleer;
    private javax.swing.JButton btnIcon;
    private javax.swing.JButton btnMaken;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblErrComm;
    private javax.swing.JLabel lblErrDatum;
    private javax.swing.JLabel lblErrNaam;
    private javax.swing.JTextArea txtCommentaar;
    private com.toedter.calendar.JDateChooser txtDatum;
    private javax.swing.JTextField txtOnderwerp;
    private com.toedter.components.JSpinField txtTotMin;
    private com.toedter.components.JSpinField txtTotU;
    private com.toedter.components.JSpinField txtVanMin;
    private com.toedter.components.JSpinField txtVanU;
    // End of variables declaration//GEN-END:variables

    private boolean veldenValideren() {
        boolean bool;
        lblErrNaam.setIcon(null);
        lblErrComm.setIcon(null);
        lblErrDatum.setIcon(null);

        if (txtOnderwerp.getText().equals("")) {
            lblErrNaam.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/symbol-error.png")));
            bool = false;
        } else if (txtCommentaar.getText().equals("")) {
            lblErrComm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/symbol-error.png")));
            bool = false;
        } else if (txtDatum.getDate() == null) {
            lblErrDatum.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/symbol-error.png")));
            bool = false;
        } else {
            bool = true;
        }
        return bool;
    }
}
